package com.RequestGongWuShu;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import com.requestGwsLongGangQu.Elevator;
import com.requestGwsLongGangQu.GwsLgqUtil;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.junit.Test;

import java.io.File;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 配电箱设备对接测试 注：方法上的注释打√的方法为对接测试环境已通过
 * 
 * @author YS
 * @date 2020/12/04 16:58
 */

@Slf4j
public class powerBox {

	private String api_key = "509ec2a899134ddab4c1797b4ed7f7f8";
	private String api_version = "1.0";
	private String engCode = "GDSYEQ-010-2019";// 工程编号
	private String projectCode = "XM20170281";// 项目编号
	private String baseUlr = "http://szwb.sz.gov.cn:2018";// 请求地址

	/**
	 * 2.1.1上报配电箱参数 √
	 */
	@Test
	public void addParams() {
		String url = baseUlr + "/openapi/powerBox/addParams";
		String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

		JSONObject json = new JSONObject();
		StringBuffer photosParam1 = new StringBuffer("ffbe6ac3b17f5a457cbd1a7abfe2ff1e");
		List<Object> photosList = new ArrayList<>();
		json.put("device_no", "7986207e559b45a3ab62611c288820qb");
		json.put("type", 2);
		json.put("name", "4#三相电流表箱");
		json.put("type_name", "三相电流表呱呱呱");
		json.put("install_company", "广东省第一建筑工程有限公司");
		json.put("installadd_type", 1);
		json.put("install_address", "某个角落");
		json.put("longitude", "25.1");
		json.put("latitude", "28.2");

		photosList.add(photosParam1);
		json.put("photos", photosList);

		json.put("temp_limit", "350.7");
		json.put("elec_limit", 23);
		json.put("around_temp", 99);

		String body = JSON.toJSONString(json);

		String signature = getSignature(api_key, api_version, engCode, projectCode, timestamp, body);
		String a = GwsLgqUtil.GwsParamUtil(json, timestamp, signature, api_key, projectCode, engCode, api_version);

		log.info("All:" + a);
		String info = HttpUtil.post(url, a);
		log.info("down!" + info);
	}

	/**
	 * 2.1.2上报配电箱状态√
	 */
	@Test
	public void addRecord() {
		String url = baseUlr + "/openapi/powerBox/addRecord";
		String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

		Map channelsParam1 = new HashMap<String, Object>();
		Map channelsParam2 = new HashMap<String, Object>();
		List<Map<String, Object>> channelsList = new ArrayList<Map<String, Object>>();

		JSONObject param = new JSONObject();
		JSONObject body = new JSONObject();
		param.put("api_key", api_key);
		param.put("project_code", projectCode);
		param.put("eng_code", engCode);
		param.put("api_version", api_version);
		param.put("timestamp", timestamp);

		body.put("device_no", "7986207e559b45a3ab62611c288820qb");
		body.put("operate_time", "2020-12-04 17:02:59");
		body.put("status", 0);
		body.put("warn_type", 0);
		body.put("warn_id", "not warn."); // WARN_TYPE 不等于 0 时此字段必填，唯一标识，同一次报警 ID 相同
		body.put("cable_a_temp", 33.2);
		body.put("cable_b_temp", 35.6);
		body.put("cable_c_temp", 34.1);
		body.put("cable_n_temp", 29.3);
		body.put("elec_limit", 23.0);
		body.put("env_temp", 14.2);

		channelsParam1.put("channel_num", "1");
		channelsParam1.put("channel_type", "2");
		channelsParam1.put("channel_value", "3");

		channelsParam2.put("channel_num", "4");
		channelsParam2.put("channel_type", "5");
		channelsParam2.put("channel_value", "6");

		channelsList.add(channelsParam1);
		channelsList.add(channelsParam2);
		body.put("channels", channelsList);

		List<JSONObject> bodyList = new ArrayList<>();// 可以传入多个body
		bodyList.add(body);
		param.put("body", bodyList);// body

		String jsonBody = JSONObject.toJSONString(bodyList);// body的json字符串
		log.info(jsonBody);
		String signature = getSignature(api_key, api_version, engCode, projectCode, timestamp, jsonBody);
		param.put("signature", signature);

		String info = HttpUtil.post(url, param.toJSONString());
		log.info(info);
	}

	/**
	 * 硬件通用上传图片√
	 */
	public void uploadImage() {
		HashMap<String, Object> paramMap = new HashMap<>();
		// 文件
		File file = new File("C:\\Users\\YS\\Desktop\\downloadImgToWebSiteWeiChat\\timg.jpg");
		paramMap.put("file", file);
		paramMap.put("api_key", api_key);
		String result = HttpUtil.post("https://szwb.sz.gov.cn:2019/uploadfile-openpai/uploadImage", paramMap);
		log.info(result);// 返回文件的id
		// resultData":"ffbe6ac3b17f5a457cbd1a7abfe2ff1e"
	}

	/**
	 * 获取签名
	 *
	 * @param: [apiKey, apiVersion, engCode, projectCode, timestamp, body]
	 * @return: java.lang.String
	 */
	public static String getSignature(String apiKey, String apiVersion, String engCode, String projectCode,
			String timestamp, String body) {
		String str4MD5 = String.format("api_key%sapi_version%sbody%seng_code=%sproject_code=%stimestamp%s", apiKey,
				apiVersion, body.isEmpty() ? "{}" : body, engCode, projectCode, timestamp);
		return getMD5Code(str4MD5);
	}

	public static String getMD5Code(String strObj) {
		String resultString = null;
		try {
			resultString = new String(strObj);
			MessageDigest md = MessageDigest.getInstance("MD5");
			Charset charset = Charset.forName("UTF-8");
			resultString = byteToString(md.digest(strObj.getBytes(charset)));
		} catch (NoSuchAlgorithmException ex) {
			ex.printStackTrace();
		}
		return resultString;
	}

	// 转换字节数组为16进制字串
	private static String byteToString(byte[] bByte) {
		StringBuffer sBuffer = new StringBuffer();
		for (int i = 0; i < bByte.length; i++) {
			sBuffer.append(byteToArrayString(bByte[i]));
		}
		return sBuffer.toString();
	}

	private static String byteToArrayString(byte bByte) {
		int iRet = bByte;
		if (iRet < 0) {
			iRet += 256;
		}
		int iD1 = iRet / 16;
		int iD2 = iRet % 16;
		return strDigits[iD1] + strDigits[iD2];
	}

	// 全局数组
	private final static String[] strDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D",
			"E", "F" };

}
